﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WeChat.PlatForm.DTO;
using WeChat.PlatForm.IService;
using WeChat.PlatForm.Common;
using WeChat.PlatForm.DTO.Member;
using Microsoft.Extensions.Logging;
using Enyim.Caching.Configuration;
using Enyim.Caching;
using WeChat.PlatForm.Common.NoSqlHelp;
using Aliyun.OSS;
using System.IO;
using System.Text;

namespace WeChat.PlatForm.API.Controllers
{
    public class CustomerController : Controller
    {
        IMemberService _member;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="member"></param>
        /// <param name="environment"></param>
        /// <param name="test"></param>
        public CustomerController(IMemberService member)
        {
            _member = member;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public async Task<JsonMsg> CustLogin()
        {
            JsonMsg msg = new JsonMsg();

            //LoggerFactory fac = new LoggerFactory();
            //MemcachedClientOptions opt = new MemcachedClientOptions();
            //opt.AddServer("39.107.114.239", 11211);
            //MemcachedClientConfiguration memconfig = new MemcachedClientConfiguration(fac, opt);
            //LoggerFactory facs = new LoggerFactory();
            //using (MemcachedClient click = new MemcachedClient(facs, memconfig))
            //{
            //    click.Set("one", "two", 3);
            //    var str = click.GetAsync<string>("one").Result;
            //}


            //RedisHelp.RedisPushLeft("good", "你好1");
            //RedisHelp.RedisPushLeft("good", "你好2");
            //RedisHelp.RedisPushRight("good", "你好3");
            //string[] strarry = RedisHelp.RedisGetRange("good");


            //var model = new { Name = "苗玉凯", Age = "12" };
            //MemberDTO memners = model.MapTo<MemberDTO>();

            int flg = await _member.ToLogin("1731796086@qq.com", "");

            msg.Status = 1;
            msg.Msg = "消息";
            msg.Data = "";
            return msg;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public JsonMsg Upload()
        {
            JsonMsg msg = new JsonMsg();

            var client = new OssClient("oss-cn-beijing.aliyuncs.com",
                "LTAIdKcxY1P0ToR2", "D2mo9waq3Lb9v7t0WPqgTP87qVcz73");

            string strEmp = string.Empty;

            string str = "a line of simple text";
            byte[] binaryData = Encoding.ASCII.GetBytes(str);
            MemoryStream requestContent = new MemoryStream(binaryData);
            PutObjectResult result = client.PutObject("mykimage", "image/mykey.txt", requestContent);

            strEmp = result.RequestId;

            //得到授权链接（最好存入redis缓存）
            //var req = new GeneratePresignedUriRequest("mykimage", "image/mykey.txt", SignHttpMethod.Get)
            //{
            //    Expiration = DateTime.Now.AddHours(1)
            //};
            //var uri = client.GeneratePresignedUri(req);

            msg.Status = 1;
            msg.Msg = "消息";
            msg.Data = "";
            return msg;
        }

        /// <summary>
        /// redis打分
        /// </summary>
        /// <returns></returns>
        public async Task<JsonMsg> RedisSoce()
        {
            JsonMsg msg = new JsonMsg();

            double one = await RedisHelp.SortedSetIncrement("mykey", "myk", 10);
            double two = await RedisHelp.SortedSetIncrement("mykey", "myk", 1);
            double thrre = await RedisHelp.SortedSetIncrement("mykey", "yzk", 2);
            double forur = await RedisHelp.SortedSetIncrement("mykey", "yzk", 1);

            Dictionary<string, double> dic = await RedisHelp.SortedSetRangeByRankWithScores("mykey", 0, 10, 0);
            msg.Status = 1;
            msg.Msg = "消息";
            msg.Data = dic;
            return msg;
        }
    }
}